﻿#pragma execution_character_set("utf-8")
#include "mainwindow.h"
#include "ui_mainwindow.h"
#ifndef MY_DEBUG_INFO
#define MY_DEBUG_INFO true
#endif

#include<iostream>
#include <QSqlDatabase>
#include<QSettings>
#include <QSqlError>
#include<QMessageBox>
#include <QFileDialog>

#include <QDateTime>
#include <QSqlQuery>
#include<QtDebug>
#include "myqsqlfactory.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    currentSimNum(0),
    totalSimNum(0)
{
    ui->setupUi(this);

    // 初始化连接数据库
    //    initialSqlCon();

    // 绑定创建表格的按钮
    connect(ui->pushButton_createTable,&QPushButton::clicked,&MyQSqlFactory::Instance(),&MyQSqlFactory::createTable);

    //绑定插入数据的按钮
    connect(ui->pushButton_insertTable,&QPushButton::clicked,&MyQSqlFactory::Instance(),&MyQSqlFactory::insertSimDataTest);

    //停止记录数据
    //    connect(ui->pushButton_stopRecord,&QPushButton::clicked,&MyQSqlFactory::Instance());

    //绑定读取数据的按钮
    //    connect(ui->pushButton_readTable,&QPushButton::clicked,&MyQSqlFactory::Instance(),&MyQSqlFactory::readSimData);

    //绑定加载离线数据的按钮
    connect(ui->pushButton_loadOffLineData,&QPushButton::clicked,this,&MainWindow::LoadOffSysInfoData);

    currentSimNum = 0;
    totalSimNum = 0;
    connect(ui->StartSimulate, &QPushButton::pressed, this, &MainWindow::SlotStartSimulate);
    connect(ui->PauseSimulate, &QPushButton::pressed, this, &MainWindow::SlotPauseSimulate);
    connect(ui->StopSimulate, &QPushButton::pressed, this, &MainWindow::SlotStopSimulate);
    connect( &timerSimulate, &QTimer::timeout, this, &MainWindow::SlotTimerSimulate);






}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::LoadOffSysInfoData()
{
    this->loadSysInfoData.ClearData();

    //打开程序运行的目录
    QString defaultPath=QCoreApplication::applicationDirPath();

    //打开选择数据文件的窗口，加载数据文件
    QString path=QFileDialog::getOpenFileName(this,tr("load file"),defaultPath,tr("text file(*.txt *.csv)"));

    //先判断一下获取的文件名空的（无效的）
    if(path.isEmpty()){
        QMessageBox::warning(this,QString("warning"),QString("路径为空"));
        return false;
    }

    //判断是否读取成功
    if(!this->loadSysInfoData.LoadFile(path)){
        QMessageBox::warning(this,QString("warning"),QString("数据读取失败"));
        return true;
    }

    //更新成功了之后，获取一下文件名字，数据的数量，当前渲染的数量
    totalSimNum=loadSysInfoData.getSysInfoDatas().size();
    currentSimNum=0;

    //获得文件名字
    QFileInfo fileInfo(path);
    loadFileName=fileInfo.fileName();

    //更新窗口控件
    ui->TotalIndex->setText(QString::number(totalSimNum));
    ui->CurrentIndex->setText(QString::number(currentSimNum));
    ui->LoadFileName->setText(loadFileName);



}

bool MainWindow::LoadTableDataByName()
{
    QString le_tableName=ui->lineEdit_tableName->text();
    bool isRead=MyQSqlFactory::Instance().readSimData(le_tableName);
    return isRead;
}

bool MainWindow::stopRecordData()
{
    /**=================by unishuai====================
    *这里还得加一些额外的判断操作，
    *类似一个标志，
    *就是通过标志来判断每一次仿真需不需要把数据插入进来
    *===================2024-05-24 ====================*/

    //先看一下这个表是不是存在
    bool isExit=MyQSqlFactory::Instance().checkIfTableExists(MyQSqlFactory::Instance().getTableName());
    if(!isExit){
        MyQSqlFactory::Instance().setTableName("");
        return true;
    }
    //如果存在看一看有没有数据
    bool isEmpty =MyQSqlFactory::Instance().isTableEmpty(MyQSqlFactory::Instance().getTableName());
    //如果没有数据，就把这个表删除
    if(isEmpty){
        MyQSqlFactory::Instance().dropTable(MyQSqlFactory::Instance().getTableName());
        MyQSqlFactory::Instance().setTableName("");
    }
    //如果有数据不干什么

}




//启动仿真
void MainWindow::SlotStartSimulate()
{
    timerSimulate.start(600);
}
//暂停仿真
void MainWindow::SlotPauseSimulate()
{
    timerSimulate.stop();
}
//停止仿真
void MainWindow::SlotStopSimulate()
{
    timerSimulate.stop();

    //将当前帧设为零
    currentSimNum = 0;
    ui->CurrentIndex->setText(QString::number(currentSimNum));
}

//定时器响应--驱动场景
void MainWindow::SlotTimerSimulate()
{
    //这里我先++
    currentSimNum++;

    //如果无加载数据，则停止
    if(loadSysInfoData.getSysInfoDatas().size() == 0)
    {
        SlotStopSimulate();
        return;
    }

    //如果当前帧已超过总帧数，则停止
    if(currentSimNum > totalSimNum)
    {
        SlotStopSimulate();
        return;
    }

    //获得当前帧的数据
    //    QVector<double> joint_list(5);
    //    joint_list[0] = m_LoadData.m_JointAngle[m_CurrentNum][0];
    //    joint_list[1] = m_LoadData.m_JointAngle[m_CurrentNum][1];
    //    joint_list[2] = m_LoadData.m_JointAngle[m_CurrentNum][2];
    //    joint_list[3] = m_LoadData.m_JointAngle[m_CurrentNum][3];
    //    joint_list[4] = m_LoadData.m_JointAngle[m_CurrentNum][4];

    SimSystemInfo ssinfo=loadSysInfoData.getSysInfoDatas()[currentSimNum-1];
    QVector<double> linearSliderPosition(6);
    linearSliderPosition[0]=ssinfo.getSlider01();
    linearSliderPosition[1]=ssinfo.getSlider02();
    linearSliderPosition[2]=ssinfo.getSlider03();
    linearSliderPosition[3]=ssinfo.getSlider04();
    linearSliderPosition[4]=ssinfo.getSlider05();
    linearSliderPosition[5]=ssinfo.getSlider06();
    QVector<double> FlightEular(3);
    FlightEular[0] =0;
    FlightEular[1] =0;
    FlightEular[2] = ssinfo.getAttack_ag();
    QVector<double> missileEular(3);
    missileEular[0] =0;
    missileEular[1] =0;
    missileEular[2] = ssinfo.getRoll_ag();



    //驱动实验
    //    SceneManager::Instance().UpdateJoint(joint_list);

    //更新数据驱动标识
    ui->CurrentIndex->setText(QString::number(currentSimNum));


    //std::cout<<"currentSimNum:"<<currentSimNum<<"/totalSimNum:"<<totalSimNum<<std::endl;
    //std::cout<<ssinfo<<std::endl;
    //std::cout<<"-----------------------------"<<std::endl;


}





















